<link rel="import" href="../bower_components/app-route/app-route.html">
<link rel="import" href="../bower_components/mist-list/mist-list.html">
<link rel="import" href="../bower_components/paper-fab/paper-fab.html">

<link rel="import" href="networks/network-create.html">
<link rel="import" href="networks/network-page.html">
<link rel="import" href="networks/network-actions.html">
<link rel="import" href="helpers/mist-lists-behavior.html">
<link rel="import" href="helpers/owner-filter-behavior.html">


<dom-module id="page-networks">
    <template>
        <style include="shared-styles">
            [hidden] {
                display: none !important;
            }
        </style>
        <app-route route="{{route}}" pattern="/:network" data="{{data}}"></app-route>
        <template is="dom-if" if="[[_isListActive(route.path)]]" restamp>
            <network-actions id="actions" items="[[selectedItems]]" actions="{{actions}}" user="[[model.user.id]]" members="[[model.membersArray]]" org="[[model.org]]">
                <mist-list selectable resizable column-menu multi-sort 
                    id="networksList" 
                    apiurl="/api/v1/networks" 
                    item-map="[[model.networks]]" 
                    name="Networks" 
                    selected-items="{{selectedItems}}" 
                    filtered-items-length="{{filteredItemsLength}}" 
                    combined-filter={{combinedFilter}} 
                    frozen=[[_getFrozenColumn()]] 
                    visible=[[_getVisibleColumns()]] 
                    renderers=[[_getRenderers()]] 
                    route={{route}} 
                    user-filter=[[model.sections.networks.q]] 
                    primary-field-name="id" 
                    filter-method="[[_ownerFilter()]]"
                    actions=[[actions]]>
                        <p slot="no-items-found">No networks found.</p>
                </mist-list>
            </network-actions>
            <div class="absolute-bottom-right" hidden$="[[!check_perm('add','network', null, model.org, model.user)]]">
                <paper-fab id="networkAdd" icon="add" on-tap="_addResource"></paper-fab>
            </div>
        </template>
        <network-create model="[[model]]" section="[[model.sections.networks]]" hidden$=[[!_isAddPageActive(route.path)]]></network-create>
        <network-page model="[[model]]" network="[[_getNetwork(data.network, model.networks, model.networks.*)]]" resource-id="[[data.network]]" section="[[model.sections.networks]]" hidden$=[[!_isDetailsPageActive(route.path)]]></network-page>
    </template>
    <script>
    Polymer({
        is: 'page-networks',
        behaviors: [
            ownerFilterBehavior,
            rbacBehavior
        ],

        properties: {
            model: {
                type: Object
            },
            actions: {
                type: Array,
                notify: true
            },
            selectedItems: {
                type: Array,
                notify: true
            }
        },

        listeners: {},

        _isAddPageActive: function(path) {
            return path == '/+add';
        },

        _isDetailsPageActive: function(path) {
            if (path && path != '/+add' && this.shadowRoot && this.shadowRoot.querySelector('network-page')) {
                this.shadowRoot.querySelector('network-page').updateState();
            }
            return path && path != '/+add';
        },

        _isListActive: function(path) {
            return !path;
        },

        _getNetwork: function(id) {
            if (this.model.networks)
                return this.model.networks[id];
        },

        _addResource: function(e) {
            this.dispatchEvent(new CustomEvent('go-to', { bubbles: true, composed: true , detail:{url: this.model.sections.networks.add}}));
        },

        _getFrozenColumn: function() {
            return ['name'];
        },

        _getVisibleColumns: function() {
            var ret = ['provider', 'machines', 'created_by', 'subnets', 'tags'];
            if (this.model.org && this.model.org.ownership_enabled == true)
                ret.splice(ret.indexOf('created_by'), 0, 'owned_by');
            return ret;
        },

        _getRenderers: function(networks, clouds) {
            var _this = this;
            return {
                'name': {
                    'body': function(item, row) {
                        return '<strong class="name">' + item + '</strong>';
                    }
                },
                'icon': {
                    'body': function(item, row) {
                        if (!_this.model.clouds[row.cloud])
                            return '';
                        return './assets/providers/provider-' + _this.model.clouds[row.cloud].provider.replace("_", "")
                            .replace(" ", "") + '.png';
                    }
                },
                'provider': {
                    'title': 'cloud',
                    'body': function(item, row) {
                        return _this.model && _this.model.clouds && _this.model.clouds[row.cloud] ?
                            _this.model.clouds[row.cloud].title : item;
                    }
                },
                'machines': {
                    'title': 'machines',
                    'body': function(item, row) {
                        var machines = Object.values(_this.model.machines).filter(function(m){ return m.network == row.id });
                        if (machines)
                            return machines.length;
                        return '';
                    }
                },
                'owned_by': {
                    'title': 'owner',
                    'body': function(item, row) {
                        return _this.model.members[item] ? _this.model.members[item].name || _this.model.members[item].email || _this.model.members[item].username : '';
                    }
                },
                'created_by': {
                    'title': 'created by',
                    'body': function(item, row) {
                        return _this.model.members[item] ? _this.model.members[item].name || _this.model.members[item].email || _this.model.members[item].username : '';
                    }
                },
                'subnets': {
                    'body': function(item, row) {
                        return item && Object.keys(item) ? Object.keys(item).length : '';
                    }
                },
                'state': {
                    'body': function(item, row) {
                        var provider = _this.model.clouds[row.cloud].provider;
                        if (provider == 'ec2')
                            return row.extra && row.extra.state != undefined ? row.extra.state : '';
                        else if (provider == 'openstack')
                            return row.admin_state_up != undefined ? 'admin state up:' + row.admin_state_up :
                                '';
                        else
                            return '';
                    }
                },
                'tags': {
                    'body': function(item, row) {
                        var tags = item,
                            display = "";
                        for (key in tags) {
                            display += "<span class='tag'>" + key;
                            if (tags[key] != undefined && tags[key] != "")
                                display += "=" + tags[key];
                            display += "</span>";
                        }
                        return display;
                    }
                }
            }
        }
    });
    </script>
</dom-module>